package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.business.domain.bo.EmployeeBo;
import com.ruoyi.business.domain.vo.EmployeeVo;
import com.ruoyi.business.domain.Employee;
import com.ruoyi.business.mapper.EmployeeMapper;
import com.ruoyi.business.service.IEmployeeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 员工Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-21
 */
@RequiredArgsConstructor
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private final EmployeeMapper baseMapper;

    /**
     * 查询员工
     */
    @Override
    public EmployeeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询员工列表
     */
    @Override
    public TableDataInfo<EmployeeVo> queryPageList(EmployeeBo bo, PageQuery pageQuery) {
        Map Mapparams=new HashMap<String,String>();
        Mapparams.put("jobNumber",bo.getJobNumber());
        Mapparams.put("fullName",bo.getFullName());
        Mapparams.put("department",bo.getDepartment());
        Mapparams.put("delFlag",bo.getDelFlag());
        Mapparams.put("outsource",bo.getOutsource());
        Mapparams.put("fujianmanage",bo.getFujianmanage());
        Mapparams.put("accommodation",bo.getAccommodation());
        Mapparams.put("entryDateStart",bo.getEntryDateStart());
        Mapparams.put("entryDateEnd",bo.getEntryDateEnd());
        Mapparams.put("housingFundStartDateStart",bo.getHousingFundStartDateStart());
        Mapparams.put("housingFundStartDateEnd",bo.getHousingFundStartDateEnd());
        Mapparams.put("insuranceStartDateStart",bo.getInsuranceStartDateStart());
        Mapparams.put("insuranceStartDateEnd",bo.getInsuranceStartDateEnd());
        Mapparams.put("insuranceEndDateStart",bo.getInsuranceEndDateStart());
        Mapparams.put("insuranceEndDateEnd",bo.getInsuranceEndDateEnd());
        Mapparams.put("firstContractExpiryDateStart",bo.getFirstContractExpiryDateStart());
        Mapparams.put("firstContractExpiryDateEnd",bo.getFirstContractExpiryDateEnd());
        Mapparams.put("secondContractExpiryDateStart",bo.getSecondContractExpiryDateStart());
        Mapparams.put("secondContractExpiryDateEnd",bo.getSecondContractExpiryDateEnd());
        Mapparams.put("thirdContractExpiryDateStart",bo.getThirdContractExpiryDateStart());
        Mapparams.put("thirdContractExpiryDateEnd",bo.getThirdContractExpiryDateEnd());
        Mapparams.put("fourthContractExpiryDateStart",bo.getFourthContractExpiryDateStart());
        Mapparams.put("fourthContractExpiryDateEnd",bo.getFourthContractExpiryDateEnd());
        Mapparams.put("fifthContractExpiryDateStart",bo.getFifthContractExpiryDateStart());
        Mapparams.put("fifthContractExpiryDateEnd",bo.getFifthContractExpiryDateEnd());
        Mapparams.put("departureDateStart",bo.getDepartureDateStart());
        Mapparams.put("departureDateEnd",bo.getDepartureDateEnd());
        Mapparams.put("freeMeal",bo.getFreeMeal());//是否包吃
        Mapparams.put("positionLevel",bo.getPositionLevel());//职务层级
        Mapparams.put("ruralMedicalInsurance",bo.getRuralMedicalInsurance());//是否缴纳农村医疗保险
        Mapparams.put("waiveSocialInsurance",bo.getWaiveSocialInsurance());//是否签订放弃五险承诺书
        Mapparams.put("insurancePacific",bo.getInsurancePacific());//太平洋保险
        Mapparams.put("insuranceCpic",bo.getInsuranceCpic());//中国人保保险
        Mapparams.put("insuranceChinalife",bo.getInsuranceChinalife());//中国人寿保险
        Mapparams.put("socialInsurance",bo.getSocialInsurance());//五险
        Mapparams.put("departureStatus",bo.getDepartureStatus());//离职情况
        Mapparams.put("idNumber",bo.getIdNumber());//身份证号
        Mapparams.put("position",bo.getPosition());//岗位
        Mapparams.put("allocated",bo.getAllocated());//申请住宿的状态
        Mapparams.put("pageSize",pageQuery.getPageSize());
        Mapparams.put("OFFSET",pageQuery.getPageSize()*(pageQuery.getPageNum()-1));
        Mapparams.put("orderByColumn",pageQuery.getOrderByColumn());
        Mapparams.put("isAsc",pageQuery.getIsAsc());
        List<EmployeeVo> List = baseMapper.getEmployeeList(Mapparams);
        Integer count=baseMapper.getEmployeeListCount(Mapparams);
        Page<EmployeeVo> result=new Page<>();
        result.setRecords(List);
        result.setTotal(count);
        result.setSize(pageQuery.getPageSize());
        result.setPages(pageQuery.getPageNum());
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工列表
     */
    @Override
    public List<EmployeeVo> queryList(EmployeeBo bo) {
        LambdaQueryWrapper<Employee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Employee> buildQueryWrapper(EmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Employee> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getJobNumber()), Employee::getJobNumber, bo.getJobNumber());
        lqw.like(StringUtils.isNotBlank(bo.getFullName()), Employee::getFullName, bo.getFullName());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), Employee::getDepartment, bo.getDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), Employee::getPosition, bo.getPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), Employee::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getEthnicity()), Employee::getEthnicity, bo.getEthnicity());
        lqw.eq(StringUtils.isNotBlank(bo.getIdNumber()), Employee::getIdNumber, bo.getIdNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getHomeAddress()), Employee::getHomeAddress, bo.getHomeAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getResidentialAddress()), Employee::getResidentialAddress, bo.getResidentialAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContactNumber()), Employee::getContactNumber, bo.getContactNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyContactPerson()), Employee::getEmergencyContactPerson, bo.getEmergencyContactPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyContactNumber()), Employee::getEmergencyContactNumber, bo.getEmergencyContactNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEducation()), Employee::getEducation, bo.getEducation());
        lqw.eq(StringUtils.isNotBlank(bo.getMajor()), Employee::getMajor, bo.getMajor());
        lqw.eq(StringUtils.isNotBlank(bo.getTechnicalTitle()), Employee::getTechnicalTitle, bo.getTechnicalTitle());
        lqw.eq(bo.getEntryDate() != null, Employee::getEntryDate, bo.getEntryDate());
        lqw.eq(bo.getDepartureDate() != null, Employee::getDepartureDate, bo.getDepartureDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartureStatus()), Employee::getDepartureStatus, bo.getDepartureStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartureReason()), Employee::getDepartureReason, bo.getDepartureReason());
        lqw.eq(bo.getFreeMeal() != null, Employee::getFreeMeal, bo.getFreeMeal());
        lqw.eq(bo.getRuralMedicalInsurance() != null, Employee::getRuralMedicalInsurance, bo.getRuralMedicalInsurance());
        lqw.eq(bo.getInsurancePacific() != null, Employee::getInsurancePacific, bo.getInsurancePacific());
        lqw.eq(bo.getInsuranceCpic() != null, Employee::getInsuranceCpic, bo.getInsuranceCpic());
        lqw.eq(bo.getInsuranceChinalife() != null, Employee::getInsuranceChinalife, bo.getInsuranceChinalife());
        lqw.eq(bo.getSocialInsurance() != null, Employee::getSocialInsurance, bo.getSocialInsurance());
        lqw.eq(bo.getInsuranceStartDate() != null, Employee::getInsuranceStartDate, bo.getInsuranceStartDate());
        lqw.eq(bo.getInsuranceEndDate() != null, Employee::getInsuranceEndDate, bo.getInsuranceEndDate());
        lqw.eq(bo.getWaiveSocialInsurance() != null, Employee::getWaiveSocialInsurance, bo.getWaiveSocialInsurance());
        lqw.eq(bo.getHousingFundStartDate() != null, Employee::getHousingFundStartDate, bo.getHousingFundStartDate());
        lqw.eq(StringUtils.isNotBlank(bo.getPositionLevel()), Employee::getPositionLevel, bo.getPositionLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeRank()), Employee::getEmployeeRank, bo.getEmployeeRank());
        lqw.eq(bo.getRankSalary() != null, Employee::getRankSalary, bo.getRankSalary());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformanceLevel()), Employee::getPerformanceLevel, bo.getPerformanceLevel());
        lqw.eq(bo.getPerformanceAmount() != null, Employee::getPerformanceAmount, bo.getPerformanceAmount());
        lqw.eq(bo.getMonthlyRent() != null, Employee::getMonthlyRent, bo.getMonthlyRent());
        lqw.eq(bo.getSkillSalary() != null, Employee::getSkillSalary, bo.getSkillSalary());
        lqw.eq(bo.getTaxSubsidy() != null, Employee::getTaxSubsidy, bo.getTaxSubsidy());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), Employee::getRemarks, bo.getRemarks());
        lqw.eq(bo.getFirstContractExpiryDate() != null, Employee::getFirstContractExpiryDate, bo.getFirstContractExpiryDate());
        lqw.eq(bo.getSecondContractExpiryDate() != null, Employee::getSecondContractExpiryDate, bo.getSecondContractExpiryDate());
        lqw.eq(bo.getThirdContractExpiryDate() != null, Employee::getThirdContractExpiryDate, bo.getThirdContractExpiryDate());
        lqw.eq(bo.getFourthContractExpiryDate() != null, Employee::getFourthContractExpiryDate, bo.getFourthContractExpiryDate());
        lqw.eq(bo.getFifthContractExpiryDate() != null, Employee::getFifthContractExpiryDate, bo.getFifthContractExpiryDate());
        lqw.eq(StringUtils.isNotBlank(bo.getContractRemarks()), Employee::getContractRemarks, bo.getContractRemarks());
        lqw.eq(bo.getAge() != null, Employee::getAge, bo.getAge());
        lqw.eq(bo.getAccommodation() != null, Employee::getAccommodation, bo.getAccommodation());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomNumber()), Employee::getRoomNumber, bo.getRoomNumber());

        lqw.orderByDesc(Employee::getJobNumber);
        return lqw;
    }

    /**
     * 新增员工
     */
    @Override
    public Boolean insertByBo(EmployeeBo bo) {
        Employee add = BeanUtil.toBean(bo, Employee.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改员工
     */
    @Override
    public Boolean updateByBo(EmployeeBo bo) {
        Employee update = BeanUtil.toBean(bo, Employee.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Employee entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除员工
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
